/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mfd2;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import java.net.Socket;
import java.io.*;
import java.net.*;
import fr.ove.openmath.mfd2.PanelMfd2;

public final class Mfd2 {
	private static final String HOST_NAME = "localhost";
	private static final int PORT_NUMBER = 6666;
	static String hostName;
	static int port;
	
	static Socket socket;

    /**
     * No-args constructor.
     */
    public Mfd2() throws IOException {
    	this(HOST_NAME, PORT_NUMBER);
    }
    
    /**
     * Constructor.
     * Instantiates the OM appletication and the appropriate communication module.
     * Registers each as listening to the other.
     *
     * @param anOMAppName name of the OM appletication
     * @param commModuleName exactly that
     * @param hostName host on which the server runs
     * @param port on which the server listens
     * @see fr.essi.sander.om.clientserver.CommFactory
     */
    public Mfd2(String hostName, int port) throws IOException {
        this.hostName = hostName;
        this.port = port;
        
        socket = new Socket(InetAddress.getByName(hostName), port);
    }
    
    public Socket getSocket() {
        return socket;
    }

    
    /**
     * Entry point for JVM.
     * This should be run
     * <OL>
     * <LI>first on the server side, e.g.,<BR>
     * java Mfd2 ServerSideSocketeer
     * <LI>then on the client side, e.g.,<BR>
     * java Mfd2 ClientSideSocketeer
     * </OL>
     *
     * @param args name of communication module to use
     * @see fr.essi.sander.om.clientserver.OMCommFactory
     */
    public static void main(String args[]) {
    	Mfd2 mfd2 = null;
    	final PanelMfd2 panelMfd2;
    	
        try {
        	switch (args.length) {
        		case 2 :
        			mfd2 = new Mfd2(args[0], Integer.parseInt(args[1]));
        			break;
        		case 0 :
        			mfd2 = new Mfd2();
        			break;
        		default :
        			usage();
        			System.exit(0);
        	}
        }
        catch (IOException ioe) {
            System.out.println("Connection to : " + mfd2.hostName + " port " + mfd2.port + " failed");
            ioe.printStackTrace();
            System.exit(0);
        }
        

    	
    	Frame mainFrame = new Frame();
    	mainFrame.setTitle("Mfd2 Running. Connected to " + mfd2.hostName + " port " + mfd2.port);
    	mainFrame.setSize(550,450);
		mainFrame.setBackground(new Color(12632256));
		panelMfd2 = new PanelMfd2(mfd2.getSocket());
    	mainFrame.add(panelMfd2);
    	mainFrame.addWindowListener(
		    new WindowAdapter() {
        		public void windowClosing(WindowEvent event) {
        		    // Probablement ici, avant de fermer la socket, envoyer l'obj OM adéquat
        		    // pour indiquer la fin de connexion
        		    
        		    event.getWindow().dispose();
                    try {
                        String object = "<OMOBJ>\n  <OMS cd=\"control\" name=\"terminate\"/>\n</OMOBJ>";
                        panelMfd2.setObject(object.getBytes());
                        panelMfd2.closeStreams();
                        socket.close();
                    }
                    catch (IOException ioe) {
                        System.out.println("Can t close socket");
                        ioe.printStackTrace();
                    }
                    
                    System.exit(0);
        		}
        	}
        );
    	
    	mainFrame.setVisible(true);
    }
    
    /**
     * Hints how to use the damn thing.
     */
    public static void usage() {
    	final String USAGE = "usage:\tjava fr.ove.openmath.mfd2.Mfd2 [hostname [port]]\n";
    	System.err.println(USAGE);
    }
    
}